﻿using Abp;
using Abp.Modules;
using LZ.DCode.Authorization.Users;
using LZ.DCode.Configuration;
using LZ.DCode.MongoDB.Configuration.Startup;
using LZ.DCode.MongoDB.MongoDb.Configuration;
using LZ.DCode.MongoDB.MongoDb.Repositories;
using LZ.DCode.Web;
using Microsoft.Extensions.Configuration;
using System.Reflection;

namespace LZ.DCode.MongoDB.MongoDb
{
    /// <summary>
    /// This module is used to implement "Data Access Layer" in MongoDB.
    /// </summary>
    [DependsOn(typeof(AbpKernelModule))]
    public class AbpMongoDbModule : AbpModule
    {
        /// <summary>
        /// 预初始化
        /// </summary>
        public override void PreInitialize()
        {
            IocManager.Register<IAbpMongoDbModuleConfiguration, AbpMongoDbModuleConfiguration>();
            //配置MonggoDb数据库地址与名称
            IAbpMongoDbModuleConfiguration abpMongoDbModuleConfiguration = Configuration.Modules.AbpMongoDb();

            var configuration = AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder());
            var ConnectionString = configuration.GetConnectionString(DCodeConsts.ConnectionStringNameMongoDb);
            abpMongoDbModuleConfiguration.ConnectionString = ConnectionString;//"mongodb://root:47HeChuang@666@47.108.62.240:27017/hc_test";
            abpMongoDbModuleConfiguration.DatabaseName = "hc_test";
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
            //注入对象实体
            IocManager.Register<MongoDbRepositoryBase<User, long>>();
        }
    }

}
